package com.own.component.elastic.core.advanced.hook;

import co.elastic.clients.elasticsearch._types.mapping.DateProperty;
import co.elastic.clients.elasticsearch._types.mapping.Property;
import com.own.component.elastic.core.advanced.aop.ElasticFieldDateTime;
import com.own.component.elastic.core.base.ElasticFieldHandlerEnum;
import com.own.component.elastic.core.base.hook.BaseElasticFieldHandler;
import com.own.component.elastic.util.JavaTimeSerializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

/**
 * ElasticFieldDateTimeHandler
 *
 * @author chenxueli
 * @date 2024-03-12 09:52:00
 */
@Slf4j
@Component
public class ElasticFieldDateTimeHandler extends BaseElasticFieldHandler<ElasticFieldDateTime> {

    /**
     * 处理
     *
     * @param annotation 注解
     * @param field      字段信息
     * @return 属性
     */
    @Override
    public Property accept(ElasticFieldDateTime annotation, Field field) {
        return Property.of(
                property -> property.date(DateProperty.of(date -> date
                        .index(true)
                        .format(JavaTimeSerializer.DATE_TIME_FORMATTER)
                ))
        );
    }

    /**
     * 操作的名称
     *
     * @return 名称
     */
    @Override
    public ElasticFieldHandlerEnum handler() {
        return ElasticFieldHandlerEnum.DATE_TIME;
    }

}
